<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.keyword" placeholder="关键字"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="search">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <!--列表-->
        <el-table :data="systemRoles" highlight-current-row v-loading="listLoading"
                  @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="name" label="角色" width="180" sortable>
            </el-table-column>
            <el-table-column prop="deleteFlag" label="状态" sortable>
                <template scope="scope">
                    <span v-if="scope.row.deleteFlag==0" style="color: green">启用</span>
                    <span v-if="scope.row.deleteFlag!=0" style="color: red">禁用</span>
                </template>
            </el-table-column>
            <el-table-column prop="createByName" label="创建人" width="120" sortable>
            </el-table-column>
            <el-table-column prop="createTime" label="创建时间" width="180" sortable>
            </el-table-column>
            <el-table-column prop="updateByName" label="更新人" width="120" sortable>
            </el-table-column>
            <el-table-column prop="updateTime" label="更新时间" width="180" sortable>
            </el-table-column>
            <el-table-column label="操作">
                <template scope="scope">
                    <el-button size="small" @click="handleAuth(scope.$index, scope.row)">授权</el-button>
                    <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next"
                           @current-change="handleCurrentChange"
                           :page-size="pageSize"
                           :total="total"
                           :current-page="currentPage"
                           style="float:right;">
            </el-pagination>
        </el-col>

        <!--新增或编辑界面-->
        <el-dialog :title="title" :visible.sync="systemRoleFormVisible" :close-on-click-modal="false">
            <el-form :model="systemRoleForm" label-width="100px" :rules="systemRoleFormRules"
                     ref="systemRoleForm">
                <el-form-item label="角色名" prop="name">
                    <el-input v-model="systemRoleForm.name" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="systemRoleFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="systemRoleSubmit" :loading="systemRoleLoading">提交
                </el-button>
            </div>
        </el-dialog>

    </section>
</template>

<script>
export default {
    data() {
        return {
            table: false,
            title: "",
            filters: {
                keyword: ''//关键字
            },
            systemRoles: [],
            total: 0,
            currentPage: 1,
            pageSize: 5,
            listLoading: false,
            sels: [],//列表选中所有行

            systemRoleFormVisible: false,//编辑界面是否显示
            systemRoleLoading: false,
            systemRoleFormRules: {
                name: [
                    {required: true, message: '请输入角色名', trigger: 'blur'}
                ]
            },
            //编辑界面数据
            systemRoleForm: {
                id: null,
                name: "",
            },
        }
    },
    methods: {
        search() {
            this.currentPage = 1;
            this.getSystemRoles();
        },

        handleCurrentChange(val) {
            this.currentPage = val;//3
            this.getSystemRoles();
        },

        selsChange: function (sels) {//当前列表页选中的所有行的数据
            this.sels = sels;
        },

        //显示编辑界面
        handleEdit: function (index, row) {
            this.title = "编辑";
            this.systemRoleFormVisible = true;//控制编辑模态框显示
            // Object.assign({}, row)  将当前行数据克隆一份
            this.systemRoleForm = Object.assign({}, row);//行数据回显
        },

        //显示新增界面
        handleAdd: function () {
            this.title = "新增";
            this.systemRoleFormVisible = true;//控制新增模态框显示
            this.systemRoleForm = {
                id: null,
                name: "",
            };
        },

        //编辑
        systemRoleSubmit: function () {
            this.$refs.systemRoleForm.validate((valid) => {
                if (valid) {
                    this.$confirm('确认提交吗？', '温馨提示', {}).then(() => {
                        this.systemRoleLoading = true;
                        let para = Object.assign({}, this.systemRoleForm);

                        this.$http.post("/system/role/save", para)
                            .then(result => {//后端执行完成了
                                this.systemRoleLoading = false;
                                result = result.data;
                                //1.提示
                                if (result.success) {
                                    this.$message({
                                        message: '操作成功！',
                                        type: 'success'
                                    });
                                } else {
                                    this.$message({
                                        message: result.message,//提示后端的业务错误
                                        type: 'error'
                                    });
                                }
                                //2.刷新页面  是否需要定位到第一
                                this.currentPage = 1;
                                this.systemRoleFormVisible = false;
                                this.getSystemRoles();
                            })
                            .catch(result => {
                                this.systemRoleLoading = false;
                                console.log(result);
                                //1.提示
                                this.$message({
                                    message: '系统繁忙，稍后重试！！！',
                                    type: 'error'
                                });
                                //2.刷新页面 要   是否需要定位到第一页
                                this.currentPage = 1;
                                this.systemRoleFormVisible = false;
                                this.getSystemRoles();
                            });
                    });
                }
            });
        },

        //删除
        handleDel: function (index, row) {
            this.$confirm('确认删除该记录吗?', '温馨提示', {
                type: 'warning'
            }).then(() => {
                this.listLoading = true;//开启列表加载忙等
                this.$http.delete("/systemRole/" + row.id)
                    .then(result => {//后端执行完成了
                        this.listLoading = false;//关闭列表加载忙等
                        result = result.data;//  AjaxResult
                        //{success:true,message:操作成功}
                        //{success:false,message:操作失败}
                        //1.提示
                        if (result.success) {
                            this.$message({
                                message: '操作成功！',
                                type: 'success'
                            });
                        } else {
                            this.$message({
                                message: result.message,//提示后端的业务错误
                                type: 'error'
                            });
                        }
                        //2.刷新页面  是否需要定位到第一
                        this.currentPage = 1;
                        this.getSystemRoles();
                    })
                    .catch(result => {
                        this.listLoading = false;//关闭列表加载忙等
                        console.log(result);
                        //1.提示
                        this.$message({
                            message: '系统繁忙，稍后重试！！！',
                            type: 'error'
                        });
                        //2.刷新页面 要   是否需要定位到第一页
                        this.currentPage = 1;
                        this.getSystemRoles();
                    });
            }).catch(() => {

            });
        },

        //批量删除
        batchRemove: function () {
            var ids = this.sels.map(item => item.id);//[1,2,3]  .toString()
            this.$confirm('确认删除选中记录吗？', '温馨提示', {
                type: 'warning'
            }).then(() => {
                this.listLoading = true;
                this.$http.patch("/systemRole", ids)
                    .then(result => {//后端执行完成了
                        this.listLoading = false;//关闭列表加载忙等
                        result = result.data;//  AjaxResult
                        //1.提示
                        if (result.success) {
                            this.$message({
                                message: '操作成功！',
                                type: 'success'
                            });
                        } else {
                            this.$message({
                                message: result.message,//提示后端的业务错误
                                type: 'error'
                            });
                        }
                        //2.刷新页面  是否需要定位到第一
                        this.currentPage = 1;
                        this.getSystemRoles();
                    })
                    .catch(result => {
                        this.listLoading = false;//关闭列表加载忙等
                        console.log(result);
                        //1.提示
                        this.$message({
                            message: '系统繁忙，稍后重试！！！',
                            type: 'error'
                        });
                        //2.刷新页面 要   是否需要定位到第一页
                        this.currentPage = 1;
                        this.getSystemRoles();
                    });
            }).catch(() => {

            });
        },

        //获取角色列表
        getSystemRoles() {
            let para = {
                currentPage: this.currentPage,//3
                pageSize: this.pageSize,
                keyword: this.filters.keyword
            };
            this.listLoading = true;//开启忙等框
            this.$http.post("/system/role/listQueryByPage", para)
                .then(result => {
                    this.listLoading = false;//关闭忙等框
                    result = result.data;//PageList
                    this.systemRoles = result.data.records;//rows
                    this.total = result.data.total;//总条数
                })
                .catch(result => {
                    console.log(result);
                    this.$message({
                        message: '系统繁忙，稍后重试！！！',
                        type: 'error'
                    });
                })
        },

    },
    mounted() {
        //页面加载完毕，调用后端拿出数据
        this.getSystemRoles();
    }
}
</script>

<style scoped>

</style>